﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
using System.Linq;
using EstateNuke.Data.Properties;

namespace EstateNuke.Data
{
    public partial class Customer
    {
        #region Fileds & Properties

        /// <summary>
        /// </summary>
        public string DisplayName
        {
            get
            {
                return string.Format("{0}({1}){2}", Name, !string.IsNullOrEmpty(Mobile) ? Mobile : Phone, IsDeleted ? " (已删除)" : "");
            }
        }

        /// <summary>
        /// </summary>
        public CustomerRequirement CustomerRequirement
        {
            get
            {
                return InternalCustomerRequirements.FirstOrDefault(customerRequirement => customerRequirement.IsDeleted == false);
            }
        }

        /// <summary>
        /// </summary>
        public CustomerLeaseRequirement CustomerLeaseRequirement
        {
            get
            {
                return InternalCustomerLeaseRequirements.FirstOrDefault(customerLeaseRequirement => customerLeaseRequirement.IsDeleted == false);
            }
        }

        /// <summary>
        /// </summary>
        internal IList<CustomerRequirement> CustomerRequirements
        {
            get
            {
                return InternalCustomerRequirements.Where(customerRequirement => customerRequirement.IsDeleted == false).ToList();
            }
        }

        /// <summary>
        /// </summary>
        internal IList<CustomerLeaseRequirement> CustomerLeaseRequirements
        {
            get
            {
                return InternalCustomerLeaseRequirements.Where(customerLeaseRequirement => customerLeaseRequirement.IsDeleted == false).ToList();
            }
        }

        /// <summary>
        /// </summary>
        public IList<Agreement> Agreements
        {
            get
            {
                return InternalAgreements.Where(agreement => agreement.IsDeleted == false).ToList();
            }
        }

        /// <summary>
        /// </summary>
        public IList<LeaseAgreement> LeaseAgreements
        {
            get
            {
                return InternalLeaseAgreements.Where(leaseAgreement => leaseAgreement.IsDeleted == false).ToList();
            }
        }

        /// <summary>
        /// </summary>
        public IList<CustomerCommunication> CustomerCommunications
        {
            get
            {
                return InternalCustomerCommunications.Where(customerCommunication => customerCommunication.IsDeleted == false).ToList();
            }
        }

        /// <summary>
        /// </summary>
        public IList<Alert> Alerts
        {
            get
            {
                return InternalAlerts.Where(alert => alert.IsDeleted == false).ToList();
            }
        }

        #endregion

        #region Create & Delete CustomerRequirement

        /// <summary>
        /// </summary>
        /// <returns></returns>
        public CustomerRequirement CreateCustomerRequirement(User modifiedUser)
        {
            if (modifiedUser == null)
            {
                throw new ArgumentNullException("modifiedUser");
            }
            var customerRequirement = InternalCustomerRequirements.FirstOrDefault();
            if (customerRequirement == null)
            {
                customerRequirement = new CustomerRequirement
                                          {
                                              RowId = Guid.NewGuid(),
                                              CreatedDate = DateTime.Now,
                                          };
                InternalCustomerRequirements.Add(customerRequirement);
            }
            customerRequirement.IsDeleted = false;
            customerRequirement.ModifiedDate = DateTime.Now;
            customerRequirement.ModifiedByUser = modifiedUser;
            return customerRequirement;
        }

        /// <summary>
        /// </summary>
        /// <param name = "modifiedUser"></param>
        public void DeleteCustomerRequirement(User modifiedUser)
        {
            if (modifiedUser == null)
            {
                throw new ArgumentNullException("modifiedUser");
            }
            var customerRequirement = CustomerRequirement;
            if (customerRequirement != null)
            {
                customerRequirement.Delete(modifiedUser);
            }
        }

        #endregion

        #region Create & Delete CustomerLeaseRequirement

        /// <summary>
        /// </summary>
        /// <returns></returns>
        public CustomerLeaseRequirement CreateCustomerLeaseRequirement(User modifiedUser)
        {
            if (modifiedUser == null)
            {
                throw new ArgumentNullException("modifiedUser");
            }
            var customerLeaseRequirement = InternalCustomerLeaseRequirements.FirstOrDefault();
            if (customerLeaseRequirement == null)
            {
                customerLeaseRequirement = new CustomerLeaseRequirement
                                               {
                                                   RowId = Guid.NewGuid(),
                                                   CreatedDate = DateTime.Now,
                                               };
                InternalCustomerLeaseRequirements.Add(customerLeaseRequirement);
            }
            customerLeaseRequirement.IsDeleted = false;
            customerLeaseRequirement.ModifiedDate = DateTime.Now;
            customerLeaseRequirement.ModifiedByUser = modifiedUser;
            return customerLeaseRequirement;
        }

        /// <summary>
        /// </summary>
        /// <param name = "modifiedUser"></param>
        public void DeleteCustomerLeaseRequirement(User modifiedUser)
        {
            if (modifiedUser == null)
            {
                throw new ArgumentNullException("modifiedUser");
            }
            var customerLeaseRequirement = CustomerLeaseRequirement;
            if (customerLeaseRequirement != null)
            {
                customerLeaseRequirement.Delete(modifiedUser);
            }
        }

        #endregion

        #region Add & Remove CustomerCommunication

        /// <summary>
        /// </summary>
        /// <returns></returns>
        public CustomerCommunication AddCustomerCommunication(User modifiedUser)
        {
            if (modifiedUser == null)
            {
                throw new ArgumentNullException("modifiedUser");
            }
            var customerCommunication = new CustomerCommunication
                                            {
                                                RowId = Guid.NewGuid(),
                                                CreatedDate = DateTime.Now,
                                            };
            InternalCustomerCommunications.Add(customerCommunication);
            customerCommunication.IsDeleted = false;
            customerCommunication.ModifiedDate = DateTime.Now;
            customerCommunication.ModifiedByUser = modifiedUser;
            customerCommunication.CommunicationDate = DateTime.Now.Date;
            return customerCommunication;
        }

        /// <summary>
        /// </summary>
        /// <param name = "customerCommunication"></param>
        /// <param name = "modifiedUser"></param>
        public void RemoveCustomerCommunication(CustomerCommunication customerCommunication, User modifiedUser)
        {
            if (customerCommunication == null)
            {
                throw new ArgumentNullException("customerCommunication");
            }
            if (modifiedUser == null)
            {
                throw new ArgumentNullException("modifiedUser");
            }
            if (InternalCustomerCommunications.Contains(customerCommunication))
            {
                customerCommunication.Delete(modifiedUser);
            }
        }

        #endregion

        #region Add & Remove Agreement

        /// <summary>
        /// </summary>
        /// <returns></returns>
        internal Agreement AddAgreement(User modifiedUser)
        {
            if (modifiedUser == null)
            {
                throw new ArgumentNullException("modifiedUser");
            }
            var agreement = new Agreement
                                {
                                    RowId = Guid.NewGuid(),
                                    CreatedDate = DateTime.Now,
                                };
            InternalAgreements.Add(agreement);
            agreement.IsDeleted = false;
            agreement.ModifiedDate = DateTime.Now;
            agreement.ModifiedByUser = modifiedUser;
            agreement.SerialNumber = string.Format("SN{0}", DateTime.Now.ToString("yyyyMMddHHmmss"));
            agreement.AgreementStateId = 0;
            agreement.SalesEmployeeId = SalesEmployeeId;
            return agreement;
        }

        /// <summary>
        /// </summary>
        /// <param name = "agreement"></param>
        /// <param name = "modifiedUser"></param>
        internal void RemoveAgreement(Agreement agreement, User modifiedUser)
        {
            if (agreement == null)
            {
                throw new ArgumentNullException("agreement");
            }
            if (modifiedUser == null)
            {
                throw new ArgumentNullException("modifiedUser");
            }
            if (InternalAgreements.Contains(agreement))
            {
                agreement.Delete(modifiedUser);
            }
        }

        #endregion

        #region Add & Remove LeaseAgreement

        /// <summary>
        /// </summary>
        /// <returns></returns>
        internal LeaseAgreement AddLeaseAgreement(User modifiedUser)
        {
            if (modifiedUser == null)
            {
                throw new ArgumentNullException("modifiedUser");
            }
            var leaseAgreement = new LeaseAgreement
                                     {
                                         RowId = Guid.NewGuid(),
                                         CreatedDate = DateTime.Now,
                                     };
            InternalLeaseAgreements.Add(leaseAgreement);
            leaseAgreement.IsDeleted = false;
            leaseAgreement.ModifiedDate = DateTime.Now;
            leaseAgreement.ModifiedByUser = modifiedUser;
            leaseAgreement.SerialNumber = string.Format("SN{0}", DateTime.Now.ToString("yyyyMMddHHmmss"));
            leaseAgreement.LeaseAgreementStateId = 0;
            leaseAgreement.SalesEmployeeId = SalesEmployeeId;
            return leaseAgreement;
        }

        /// <summary>
        /// </summary>
        /// <param name = "leaseAgreement"></param>
        /// <param name = "modifiedUser"></param>
        internal void RemoveLeaseAgreement(LeaseAgreement leaseAgreement, User modifiedUser)
        {
            if (leaseAgreement == null)
            {
                throw new ArgumentNullException("leaseAgreement");
            }
            if (modifiedUser == null)
            {
                throw new ArgumentNullException("modifiedUser");
            }
            if (InternalLeaseAgreements.Contains(leaseAgreement))
            {
                leaseAgreement.Delete(modifiedUser);
            }
        }

        #endregion

        #region Ensure

        /// <summary>
        /// </summary>
        public void Ensure()
        {
            if (string.IsNullOrEmpty(Name))
            {
                throw new EntityException(string.Format(CultureInfo.CurrentCulture, Resources.NotNullAllowed, RowId, "Customer", "Name"));
            }
            if (string.IsNullOrEmpty(Mobile))
            {
                throw new EntityException(string.Format(CultureInfo.CurrentCulture, Resources.NotNullAllowed, RowId, "Customer", "Mobile"));
            }
            if (Age < 0)
            {
                throw new EntityException(string.Format(CultureInfo.CurrentCulture, Resources.NonNegativeNumberAllowed, RowId, "Customer", "Age"));
            }
            Name = Name.Length > 64 ? Name.Substring(0, 64) : Name;
            Gender = string.IsNullOrEmpty(Gender) ? null : Gender.Length > 1 ? Gender.Substring(0, 1) : Gender;
            IdentityNumber = string.IsNullOrEmpty(IdentityNumber) ? null : IdentityNumber.Length > 32 ? IdentityNumber.Substring(0, 32) : IdentityNumber;
            QQ = string.IsNullOrEmpty(QQ) ? null : QQ.Length > 32 ? QQ.Substring(0, 32) : QQ;
            Phone = string.IsNullOrEmpty(Phone) ? null : Phone.Length > 32 ? Phone.Substring(0, 32) : Phone;
            Mobile = Mobile.Length > 32 ? Mobile.Substring(0, 32) : Mobile;
            Email = string.IsNullOrEmpty(Email) ? null : Email.Length > 256 ? Email.Substring(0, 256) : Email;
            PostalCode = string.IsNullOrEmpty(PostalCode) ? null : PostalCode.Length > 8 ? PostalCode.Substring(0, 8) : PostalCode;
            Address = string.IsNullOrEmpty(Address) ? null : Address.Length > 128 ? Address.Substring(0, 128) : Address;
            Comment = string.IsNullOrEmpty(Comment) ? null : Comment.Length > 512 ? Comment.Substring(0, 512) : Comment;
        }

        #endregion

        #region Delete

        /// <summary>
        /// </summary>
        public void Delete(User modifiedUser)
        {
            if (modifiedUser == null)
            {
                throw new ArgumentNullException("modifiedUser");
            }
            IsDeleted = true;
            ModifiedDate = DateTime.Now;
            ModifiedByUser = modifiedUser;
            ((List<CustomerCommunication>) CustomerCommunications).ForEach(agreement => agreement.Delete(modifiedUser));
            ((List<Agreement>) Agreements).ForEach(agreement => agreement.Delete(modifiedUser));
            ((List<LeaseAgreement>) LeaseAgreements).ForEach(leaseAgreement => leaseAgreement.Delete(modifiedUser));
            ((List<CustomerRequirement>) CustomerRequirements).ForEach(customerRequirement => customerRequirement.Delete(modifiedUser));
            ((List<CustomerLeaseRequirement>) CustomerLeaseRequirements).ForEach(customerLeaseRequirement => customerLeaseRequirement.Delete(modifiedUser));
        }

        /// <summary>
        /// </summary>
        public void CancelDelete(User modifiedUser)
        {
            if (modifiedUser == null)
            {
                throw new ArgumentNullException("modifiedUser");
            }
            IsDeleted = false;
            ModifiedDate = DateTime.Now;
            ModifiedByUser = modifiedUser;
        }

        #endregion
    }
}